package com.chinatechstar.file.controller;

import java.io.IOException;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.chinatechstar.component.commons.result.ActionResult;
import com.chinatechstar.component.commons.result.ListResult;
import com.chinatechstar.component.commons.result.ResultBuilder;
import com.chinatechstar.component.commons.utils.FileUtils;
import com.chinatechstar.file.service.FileService;
import com.chinatechstar.file.vo.FileVO;

/**
 * 文件信息的控制层
 *
 */
@Api(value = "文件信息的控制层", tags = {"文件信息"})
@RestController
@RequestMapping("/file")
public class FileController {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private FileService fileService;

    /**
     * 查询文件分页
     *
     * @param fileVO 文件前端参数
     * @return
     */
    @ApiOperation(value = "查询文件分页", tags = {"文件信息"})
    @GetMapping(path = "/queryFile")
    public ListResult<Object> queryFile(FileVO fileVO) {
        Map<String, Object> data = fileService.queryFile(fileVO.getCurrentPage(), fileVO.getPageSize(), fileVO.getOriginalFilename(), fileVO.getSorter());
        return ResultBuilder.buildListSuccess(data);
    }

    /**
     * 删除文件
     *
     * @param id 文件ID
     * @return
     */
    @ApiOperation(value = "删除文件", tags = {"文件信息"})
    @PostMapping(path = "/deleteFile")
    public ActionResult deleteFile(@RequestParam(name = "id") Long[] id) throws IOException {
        fileService.deleteFile(id);
        return ResultBuilder.buildActionSuccess();
    }

    /**
     * 上传文件
     *
     * @param file       文件资源
     * @param id         用户ID
     * @param uploadType 上传类型
     * @return
     */
    @ApiOperation(value = "上传文件", tags = {"文件信息"})
    @PostMapping(value = "/uploadFile", consumes = {"multipart/form-data"})
    public ActionResult uploadFile(@RequestParam(name = "file") MultipartFile file, @RequestParam(name = "id") Long id,
                                   @RequestParam(name = "uploadType", required = false) String uploadType) throws IOException {
        fileService.uploadFile(file, id, uploadType);
        return ResultBuilder.buildActionSuccess();
    }

    /**
     * 下载文件
     *
     * @param request          请求对象
     * @param response         响应对象
     * @param originalFilename 文件名称
     * @param url              下载URL
     */
    @ApiOperation(value = "下载文件", tags = {"文件信息"})
    @PostMapping(path = "/downloadFile")
    public void downloadFile(HttpServletRequest request, HttpServletResponse response,
                             @RequestParam(name = "originalFilename") String originalFilename, @RequestParam(name = "url") String url) {
        try {
            FileUtils.downloadFile(originalFilename, url, response);
        } catch (Exception e) {
            logger.warn(e.toString());
        }
    }


}
